﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Planning.Classes;
using System.Security.Cryptography;

namespace Planning.Forms
{
    public partial class loginForm : Form
    {
        #region Construct
        public loginForm() {
            InitializeComponent();
        }
        #endregion

        #region Clics
        private void loginForm_FormClosing(object sender, FormClosingEventArgs e) {
            Environment.Exit(0);
        }

        private void menuQuitter_Click(object sender, EventArgs e) {
            Environment.Exit(0);
        }

        private void menuOptions_Click(object sender, EventArgs e) {
            // Ouvre la fenêtre des paramètres
            parametresForm form = new parametresForm();
            form.Show();
        }

        private void connexion_Click(object sender, EventArgs e) {
            using (MD5 md5hash = MD5.Create()) {
                // Récupère le hash du mot de passe en MD5
                string hash = getMd5Hash(md5hash, pwdText.Text);
                // Si la connexion avec le nom d'utilisateur et le hash réussi
                if (SQL.checkLogin(usernameText.Text, hash)) {
                    // Récupère les informations sur l'utilisateur
                    DataTable infoConnexion = SQL.requete("SELECT * FROM CONNEXION, ADHERENT, ASSOCIATION WHERE CONNEXION.id_adherent = ADHERENT.id_adherent AND ADHERENT.id_association = ASSOCIATION.id_association AND login_connexion = \'" + usernameText.Text + "\';");
                    // Stocke la ligne numéro 0
                    DataRow info = infoConnexion.Rows[0];
                    // Affiche le planning avec la ligne correspondante aux infos de connexion
                    planningForm form = new planningForm(info);
                    form.Show();
                    this.Hide();
                }
                // Si la connexion écoue
                else {
                    MessageBox.Show("Erreur lors de la connexion");
                }
            }
        }

        private void planning_Click(object sender, EventArgs e) {
            // Affiche le planning en mode invité
            planningForm form = new planningForm();
            form.Show();
            this.Hide();
        }
        #endregion

        private static string getMd5Hash(MD5 md5Hash, string input) {
            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++) {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }

        private void menuApropos_Click(object sender, EventArgs e)
        {
            aboutForm form = new aboutForm();
            form.Show();
        }
    }
}
